﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CrushCollider : MonoBehaviour {
	
	private List<Vector3> contacts = new List<Vector3>();

	// Use this for initialization
	void Start () {
	
	}

	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.GetComponent<OrbCollectible>())
			return;

		//SphereCast(origin: Vector3, radius: float, direction: Vector3, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers)

		RaycastHit[] hits;
		float hitDistance = 1.0f;
		float hitRadius = 0.42f;
		Vector3 direction;

		bool topHit = false;
		direction = new Vector3(0, 1, 0);
		hits = Physics.SphereCastAll(transform.position + direction * -hitRadius , hitRadius, direction, Mathf.Infinity);
		for(int i = 0; i < hits.Length; ++i)
		{
			if(hits[i].distance <= hitDistance)
			{
				topHit = true;
				break;
			}
		}

		bool bottomHit = false;
		direction = new Vector3(0, -1, 0);
		hits = Physics.SphereCastAll(transform.position + direction * -hitRadius, hitRadius, direction, Mathf.Infinity);
		for(int i = 0; i < hits.Length; ++i)
		{
			if(hits[i].distance <= hitDistance)
			{
				bottomHit = true;
				break;
			}
		}
		if(topHit && bottomHit)
			transform.parent.GetComponent<Plateformer>().Crush(true);


		bool rightHit = false;
		direction = new Vector3(1, 0, 0);
		hits = Physics.SphereCastAll(transform.position + direction * -hitRadius , hitRadius, direction, Mathf.Infinity);
		for(int i = 0; i < hits.Length; ++i)
		{
			if(hits[i].distance <= hitDistance)
			{
				rightHit = true;
				break;
			}
		}
		
		bool leftHit = false;
		direction = new Vector3(-1, 0, 0);
		hits = Physics.SphereCastAll(transform.position + direction * -hitRadius, hitRadius, direction, Mathf.Infinity);
		for(int i = 0; i < hits.Length; ++i)
		{
			if(hits[i].distance <= hitDistance)
			{
				leftHit = true;
				break;
			}
		}
		if(rightHit && leftHit)
			transform.parent.GetComponent<Plateformer>().Crush(false);
	}

	// Update is called once per frame
	void Update () {
	
	}
}
